﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using MedLink2011.Controls;
using MedLink2011.Views.Patients.ViewModels;
using MedLink2011.Web.Models;

namespace MedLink2011.Views.Dashboard
{
    public partial class DashboardP : Page
    {
        PatientViewModel _patientViewModel = new PatientViewModel();
        ConsultationViewModel _consultationViewModel = new ConsultationViewModel();
        DiagnoViewModel _diagoViewModel = new DiagnoViewModel();
        PrescriptionViewModel _prescriptionViewModel = new PrescriptionViewModel();
        LabViewModel _labViewModel = new LabViewModel();
        public DashboardP()
        {
            InitializeComponent();
            LoadViewModels();
            BindBusyIndicators();

        }

       public void LoadViewModels()
       {
           patientDataForm.DataContext = _patientViewModel;
           consultationsDataGrid.DataContext = _consultationViewModel;
           diagnosisDataGrid.DataContext = _diagoViewModel;
           prescriptionDataGrid.DataContext = _prescriptionViewModel;
           labTestDataGrid.DataContext = _labViewModel;
           ChartBMI.DataContext = _consultationViewModel;
           ChartBloodPressure.DataContext = _consultationViewModel;
       }

       public void BindBusyIndicators()
       {
           ConsultationBusyIndicator.DataContext = _consultationViewModel;
           DiagnoBusyIndicator.DataContext = _diagoViewModel;
           PrescriptionBusyIndicator.DataContext = _prescriptionViewModel;
           LabBusyIndicator.DataContext = _labViewModel;
           PatientBusyIndicator.DataContext = _patientViewModel;
       }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void GetPhotoButtonClick(object sender, System.Windows.RoutedEventArgs e)
        {
            var webcam = new WebcamWindow();
            webcam.Show();
            webcam.Closed += WebcamClosed;
        }
        void WebcamClosed(object sender, EventArgs e)
        {
            var window = sender as WebcamWindow;
            if (window.DialogResult == true)
            {
                ((Patient) patientDataForm.CurrentItem).Photo = window.picturesListBox.SelectedItem as byte[];
            }
        }

        private void patientDataForm_EditEnded(object sender, DataFormEditEndedEventArgs e)
        {
            if (e.EditAction == DataFormEditAction.Commit)
            {
                _patientViewModel.SavePatient();
            }

        }

    }
}
